<?php
/**
 * Runs in response to a form from view.php. Adds an item to the
 * given resourcepage instance, then redirects back to view with
 * editing mode turned on for that item.
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 *//** */
  
require_once("module_resourcepage.php");
$module=module_resourcepage::$inst;
 
$id=required_param('id',PARAM_INT); // Course Module ID
$canned=required_param('canned',PARAM_ALPHA);
$action=required_param('action',PARAM_ALPHA);
$scrollpos=optional_param('scrollpos',0,PARAM_INT);

$module->obtain_view_info($id,null,$cm,$course,$resourcepage);
    
// Security checks
$module->verify_edit_action($cm);

// Check request
if($action=='add') {
    $position=required_param('position',PARAM_INT);
    if($position<0 || $position>1) {
        error('Unexpected position: '.$position);
    }
    try {
        $module->add_canned_usage($resourcepage->id,$canned,$position);
    } catch(Exception $e) {
        error(
            'Failed to add canned item. (This can happen if you are '.
            'editing the same page in two windows and tried to add the '.
            'item twice.)'); 
    }
    $logtext='Added canned text '.$canned;
} else if($action=='delete') {
    $module->delete_canned_usage($resourcepage->id,$canned);
    $logtext='Deleted canned text '.$canned;
} else {
    error('Unknown action: '.$action);
}
    
// Log and return to view
add_to_log($course->id, "resourcepage", "additem", "view.php?id=$cm->id",
    $logtext,$cm->id); // TODO
redirect('view.php?id='.$cm->id.($scrollpos ? '&scrollpos='.$scrollpos : ''));
 
?>